I’m trying to run a worker that returns some HTML on GET /
and from that HTML on click on a button make a POST call to /langchainQA to use a Q&A chain to answer questions about a given document.
How the code looks like:
// imports
import { Router } from 'itty-router';
import { OpenAI } from 'langchain/llms/openai';
import { FaissStore } from 'langchain/vectorstores/faiss';
import { OpenAIEmbeddings } from 'langchain/embeddings/openai';
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
const router = Router();
const model = new OpenAI({
  temperature: 0,
  openAIApiKey: 'APIKEY',
});
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
const embeddings = new OpenAIEmbeddings({
  openAIApiKey: 'APIKEY',
});
const HTML = 
 ... 
router.get('/', async () => {
  return new Response(html, {
    headers: {
      'content-type': 'text/html;charset=UTF-8',
    },
  });
});
router.post('/qa', async (request) => {
  const body = await request.json();
  const { question, pdfContent } = body;
  const chunks = await textSplitter.splitText(pdfContent);
  const knowledgeStore = await FaissStore.fromTexts(chunks, {}, embeddings);
  const docs = knowledgeStore.similaritySearch(question);
  const chain = loadQAChain(model);
  const res = chain.run({ question, docs });
  const answer = res.answer;
  return new Response(JSON.stringify(answer), {
    headers: { 'content-type': 'application/json' },
  });
});
On const knowledgeStore = await FaissStore.fromTexts(chunks, {}, embeddings);
I’m getting this error:
✘ [ERROR] Build failed with 2 errors:
node_modules/langchain/dist/vectorstores/faiss.js:94:32:
ERROR: Could not resolve “node:fs/promises”
node_modules/langchain/dist/vectorstores/faiss.js:95:34:
ERROR: Could not resolve “node:path”
I have node_compat=true in my wrangler config.
Any idea how to fix the unresolved imports?
Thanks!
https://cdn.discordapp.com/attachments/1110650948818321449/1110650949346787441/Screenshot_2023-05-23_at_3.03.36_PM.png▾